package com.seelyn.ddvideo.web;

import com.seelyn.ddvideo.model.VideoPrefix;
import com.seelyn.ddvideo.service.VideoPrefixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ifcousing-linfeng
 * @since 2017/6/12
 */
@RestController
@RequestMapping("/api")
public class VideoPrefixController {

    @Autowired
    private VideoPrefixService videoPrefixService;

    @PostMapping("/video/prefix")
    public View<VideoPrefix> savePrefix(@RequestParam("from") String from, @RequestParam("prefix") String prefix) {

        VideoPrefix videoPrefix = videoPrefixService.findByFromSite(from);
        if (videoPrefix != null) {
            videoPrefix.setPrefixUrl(prefix);
        } else {
            videoPrefix = new VideoPrefix();
            videoPrefix.setFromSite(from);
            videoPrefix.setPrefixUrl(prefix);
        }
        videoPrefixService.saveVideoPrefix(videoPrefix);
        View<VideoPrefix> result = new View<>();
        result.setCode(View.OK);
        result.setMessage("OK");
        result.setData(videoPrefix);
        return result;

    }

    @GetMapping("/video/prefix")
    public View<List<VideoPrefix>> listPrefix() {

        View<List<VideoPrefix>> result = new View<>();
        result.setCode(View.OK);
        result.setMessage("OK");
        List<VideoPrefix> videoPrefix = videoPrefixService.findAll();
        result.setData(videoPrefix);
        return result;

    }

}
